import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store"
Vue.use(VueRouter)
//为解决NavigationDuplicated（导航重复）问题
let originPush=VueRouter.prototype.push
let originRepelace=VueRouter.prototype.replace
VueRouter.prototype.push=function(location,resolve,reject){
    if(resolve&&reject){
        originPush.call(this,location,resolve,reject)
    }else{
        originPush.call(this,location,()=>{},()=>{})
    }
}
VueRouter.prototype.replace=function(location,resolve,reject){
    if(resolve&&reject){
        originRepelace.call(this,location,resolve,reject)
    }else{
        originRepelace.call(this,location,()=>{},()=>{})
    }
}

import Login from "@/pages/Login"
import Search from "@/pages/Search"
import Home from "@/pages/Home"
import Register from "@/pages/Register"
import Detail from "@/pages/Detail"
import AddCartSuccess from "@/pages/AddCartSuccess"
import shopCart from "@/pages/ShopCart"
import Trade from "@/pages/Trade"
import Pay from "@/pages/Pay"
import paysuccess from "@/pages/PaySuccess"
import Center from "@/pages/Center"
import myOrder from '@/pages/Center/myOrder'
import ourOrder from '@/pages/Center/ourOrder'
const router=new VueRouter({
            routes:[
                {
                    path:"/detail/:skuId",
                    component:Detail,
                },
                {
                    path:"/paysuccess",
                    component:paysuccess,
                },
                {
                    path:"/center",
                    component:Center,
                    meta:{
                        show:true
                    },
                    children:[
                     {
                         path:'myorder',
                         component:myOrder
                     },
                     {
                        path:'ourorder',
                        component:ourOrder
                    },
                    {
                        path:'/',
                        redirect:'/center/myorder',
                        
                    }
                    ]
                },
                {
                    path:"/trade",
                    component:Trade,
                },
                {
                    path:"/pay/:orderId",
                    component:Pay,
                },
                {
                    path:"/shopcart",
                    component:shopCart,
                },
                {
                    path:"/login",
                    component:Login,
                },
                {
                    name:"addcartsuccess",
                    path:"/cart/addcartsuccess",
                    component:AddCartSuccess,
                    meta:{
                        show:true
                    }

                },
                {
                    //params占位
                    path:"/search/:keyword?",
                    component:Search,
                    name:'search', 
                    meta:{
                        show:true
                    }
                },
                {
                    path:"/home",
                    component:Home,
                    meta:{
                        show:true
                    }
                },
                {
                    path:"/register",
                    component:Register,
                   
                },
                {
                    path:"/",
                    redirect:"/home",
                   
                },

            ]
})
//全局守卫 前置
router.beforeEach(async(to,from,next)=>{
    //拿取token
    let token = store.state.login.token
    //拿取 用户信息
    let name = store.state.login.userInfo.name
    if(token){
        //思路：有token，不能再登录了（跳到首页），其他页面ok
                                    //有name，其他都行，没有name 发请求（获取到了，其他都行；没获取到，发退出，去登录页面）
        if(to.path=="/login"){
            next('/home')
        }else{
            if(name){
                next()
            }else{
                try {
                    await store.dispatch('getUserInfo')
                    next()
                } catch (error) {
                    await store.dispatch('getLogOut')
                    next('/login')
                }
            }
        }
    }else{
    //没有token，不能去交易、支付、支付成功页面,去了直接跳到登录，登录了再返回原来页面
    let toPath = to.path;
    //判断未登录：去trade、去支付、去支付成功、去个人中心【我的订单、团购订单】
    if (toPath.indexOf('trade') != -1 || toPath.indexOf('pay') != -1 || toPath.indexOf('center') != -1) {
         //判断未登录：去trade、去支付、去支付成功、去个人中心【我的订单、团购订单】
         //跳转到登录页
         next('/login?redirect='+toPath);
    } else {
         //去的并非上面这些路由,放行
         next();
    }
    }

})

export default router